package br.ucb.sigplan.action.impl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ucb.sigplan.action.Action;
import br.ucb.sigplan.dao.impl.ProfessorDAOImpl;
import br.ucb.sigplan.util.RecuperaSenhaUtil;
import br.ucb.sigplan.validator.Validator;
import br.ucb.sigplan.beans.*;

public class RecuperaSenhaAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<String> erros = Validator.validaEsqueceSenha(req);
		
		if (!erros.isEmpty()) {
			req.setAttribute("erros", erros);
			return "/esqueceuSenha.jsp";
		}
		
		String email = req.getParameter("email");
		
		ProfessorDAOImpl dao = new ProfessorDAOImpl();
		Professor pr = dao.findByEmail(email);
		
		if (pr != null){
			req.setAttribute("msg", "emailConfirmacaoResetarSenha");
			RecuperaSenhaUtil.enviaEmailRedefinirSenha(pr.getEmail(), "Recuperacao de Senha", req, pr.getMatricula());
		}	
		
		return "/login.jsp";
	}
	

}
